
$(function(){
	update_login_status();
	
	
	//问答列表内容切换
	$('#faq-list li > a,#faq-list2 li > a').click(
		function(){
			$(this).next().toggle()
			.parent('li').toggleClass('show').siblings().removeClass('show').find('p').hide();
			return false;
		}
	);
	
	$.getJSON('/api/t_total.php',function(ticket_amount){
		$('#ticket-amount').html(ticket_amount);
	});
	
	//$('#navi li').eq($('#navi').attr('current')).addClass('current').siblings().removeClass('current');
});

function feedback(){
	popup({title:'意见反馈',url:'/feedback.php'});
}

function feedback_reply(fid){
	//popup({title:'意见反馈',url:'/feedback.php'});
	popup({title:'意见反馈',url:'/admin/feedbacks_list.php?action=reply&id='+fid});
}



function update_login_status(){
	$.get("/account/userstatus.php?r=" + Math.random(),function(data){
		arr=data.split("|");
		//str = '<a href="/account/">[我的资料]</a>';
		str='';
		if(arr[0]){
			str = '欢迎您，<b>'+ arr[1] +'</b>' + str + '<a href="javascript:logoff();">[退出]</a>';
			$('#welcome').hide();
		}
		else $('#welcome').show();
		var statusbar = $("#userstatus");
		if(statusbar) statusbar.html(str);
		
	});
}

function logoff(){
	$.get("/account/login.php?action=logoff&r=" + Math.random(),function(data){
			update_login_status();
			//document.location.href = "/";
		}
	);
}

function orderlogin(callback)
{	if(typeof(callback) =="function")
	    window.login_success_callback = callback;
	popup({url:'/account/login.php?from=ajax&order=1',title:"登录"});
}



function login(callback)
{	if(typeof(callback) =="function")
	    window.login_success_callback = callback;
	popup({url:'/account/login.php?from=ajax',title:"登录"});
}

function popup(data)
{
	hide_selects();
	$('.popup_error').remove();
	data.width = 555;
	if (!data.height) data.height = 100;
	if (window.popup_last_height) data.height = window.popup_last_height;
	if (!data.title) data.title = 'Huoche';
	var body = document.documentElement || document.body;
	//获得页面尺寸信息
	var page = get_page_size();
	//如果没有黑色背景
	if ($('#popup_bg').length == 0)
	{
		$('<div id="popup_bg"></div>').css(
		{
			height: page.height,
			width: data.width,
			width: $(window).width(),
			zIndex: '99999',
			position:'absolute',
			top:0,
			left:0,
			padding:0,
			backgroundColor:'#000000',
			margin:'0px'
		})
		.css('opacity',0)
		.attr('popup','yes')
		.bind('click',close_popup)
		.appendTo(document.body)
		.fadeTo(300,0.7);
	}
	
	//计算窗口位置
	data.top = page.scrollTop + ($(window).height() - data.height ) / 2 - 50;
	data.left =  ($(window).width() - data.width ) / 2;

	//删除上次弹出的窗口
	$('#popup').remove();
	
	//创建新的弹出窗口
	var div = document.createElement('div');
	$(div)
		.css(
		{
			width: data.width,
			zIndex: 100000,
			backgroundColor: 'transparent',
			position:'absolute',
			padding:'0px',
			margin:'0px',
			opacity:0.98,
			overflow:'hidden',
			top:data.top,
			left:data.left
		})
		.attr('popup','yes');
	
	$(div)
		.attr('id','popup')
		.html('<div id="popup_top_wrapper"><div class="popup_top_bar"><img src="images/blank.gif"/></div>'
			+'<div class="popup_top" id="popup_top">'
	    	+'<span class="popup_title"><strong id="popup_title">'+data.title+'</strong></span>'
	        +'<a class="popup_close_button" href="javascript:close_popup();"></a>'
	    	+'</div></div><div class="popup_content">'
	    	+'<div class="popup_main">'
			+'<div id="popup_content" style="overflow:auto;height:'+parseInt(data.height)+'px;'
			+'background:transparent url(images/loading.gif) no-repeat 50% 50%;">'
	        +'<div id="popup_content_inner"></div></div></div>  </div>'
			+'<div class="popup_bottom"><img src="images/blank.gif"/></div>')
		.attr('source_src',data.url);
		
	$(document.body).append(div);
	
	show_msg = function(s)
	{
		//动态适应高度
		var size_to_height = function()
		{
			var page = get_page_size();
			$('#popup_content_inner').css('height','10').html(s);
			setTimeout(function()
			{
				var height = $('#popup_content_inner').get(0).scrollHeight+2;
				//document.title = window.popup_last_height + ' '+ height;
				window.popup_last_height = height;
				$('#popup_content_inner').height(height);

				$('#popup_content')
					.css('background','#ffffff none no-repeat -1000px 0px');

				var top = page.scrollTop + ($(window).height() - height ) / 2 - 50;
				
				$('#popup').animate({top:top},300);
				$('#popup_content').css('overflow','hidden').animate(
					{
						opacity:1,
						height:height
					},300);
			},0);
			
		}
		
		//如果IE6 或者 IE7 关闭动画效果，直接显示
		if ($.browser.msie && ($.browser.version == 6 || $.browser.version == 7))
		{
			size_to_height();
		}
		else
		{
			$('#popup_content').fadeTo(300,0,size_to_height);
		}
		
	};
	if (data.url){{
		$.get(data.url,{ },show_msg);}
	}else if (data.content)
		show_msg(data.content);
}

function show_popup_loading()
{
	$('#popup_content').css('background','#fff url(images/loading.gif) no-repeat 50% 50%');
	$('#popup_content_inner').hide();
	$('.popup_error').hide();
}

function hide_popup_loading()
{
	$('#popup_content').css('background','#fff none no-repeat 50% 50%');
	$('#popup_content_inner').show();
	$('.popup_error').show();
}

function close_popup()
{
	show_selects();
	window.popup_last_height = $('#popup_content').height();
	$('[popup]').fadeOut('fast',function()
	{
		$('[popup]').remove();
		$('.popup_error').remove();
	});
	
}

function popup_resize(callback)
{
	var page = get_page_size();
	var height = $('#popup_content_inner').get(0).scrollHeight+2;
	if (height != window.popup_last_height)
	{
		window.popup_last_height = height;
		$('#popup_content_inner').height(height);
		var top = page.scrollTop + ($(window).height() - height ) / 2 - 50;
	
		$('#popup').animate({top:top},300);
		$('#popup_content').css('overflow','hidden').animate(
			{
				opacity:1,
				height:height
			},300,callback);
	}
	else callback();
}

function hide_selects()
{
	$('select').hide();
}

function show_selects()
{
	$('select').show();
}

function get_page_size()
{
	var re = {};
	if (document.documentElement && document.documentElement.clientHeight)
	{
		var doc = document.documentElement;
		re.width = (doc.clientWidth>doc.scrollWidth)?doc.clientWidth:doc.scrollWidth;
		re.height = (doc.clientHeight>doc.scrollHeight)?doc.clientHeight:doc.scrollHeight;
		re.scrollTop = (doc.scrollTop>document.body.scrollTop)?doc.scrollTop:document.body.scrollTop;
		re.scrollHeight = (doc.scrollHeight>document.body.scrollHeight)?doc.scrollHeight:document.body.scrollHeight;
	}
	else
	{
		var doc = document.body;
		re.width = (window.innerWidth>doc.scrollWidth)?window.innerWidth:doc.scrollWidth;
		re.height = (window.innerHeight>doc.scrollHeight)?window.innerHeight:doc.scrollHeight;
		re.scrollTop = doc.scrollTop;
		re.scrollHeight = doc.scrollHeight;
	}
	return re;
}

function show_error(content,o,id)
{
	var animation = 300;
	id += 'error_box';
	if ($('#'+id).length > 0) animation = 0;
	$('#'+id).remove();
	o = $(o);
	//偏移分量
	x = parseInt(o.attr('offsetx'));
	if (!x || isNaN(x)) x = 20;
	var html = '<div class="errtip_periphery">'
    	+'<div class="errtip_side_top"><img src="images/error_top.gif"/></div>'
        +'<div class="error_main">'
        	+'<div class="error , red">'
			+content
          +'  </div>'
        +'</div>'
        +'<div class="errtip_side_bottom"><img src="images/error_bottom.gif"/></div>'
    +'</div>';
	$('<div class="popup_error" id="'+id+'"></div>').html(html).appendTo(document.body).css({
		top:parseInt(o.offset().top)-8,
		left:parseInt(o.offset().left) + parseInt(o.width()) + x,
		zIndex:100001
	}).fadeIn(animation);
}

function hide_error(o,id)
{
	id += 'error_box';
	$('#'+id).remove();
	o = $(o);
	var html = '<img class="correct_img" src="images/right_pic.gif" />';
	$('<div class="popup_error" id="'+id+'"></div>').html(html).appendTo(document.body).css({
		top:parseInt(o.offset().top)-8,
		left:parseInt(o.offset().left) + parseInt(o.width()) + 20,
		zIndex:100001
	}).fadeIn(300);
}

//----------------------------------------------------------------------------
//计算费用
function calculate_money( unitPrice, amount, carrigeFee, serviceFee, payserviceFee ){
	var ticketsFee = unitPrice*amount + serviceFee*amount + carrigeFee;
	var totalMoney = ticketsFee + ticketsFee*payserviceFee;
	return typeof payserviceFee == 'undefined' ? ticketsFee : totalMoney;
}

//----------------------------------------------------------------------------

//TOP帮助中心的下拉
function toggle(targetid){
      
         target=document.getElementById(targetid);
             if (target.className=="downbox"){
                 target.className="downbox current";
             } else {
                 target.className="downbox";
             						}
     	 
}
// 设置首页  
function setHomepage(){　 
    if (document.all){  
        document.body.style.behavior = 'url(#default#homepage)';  
        document.body.setHomePage('http://piao.huoche.com');  
    }else if (window.sidebar){  
        if (window.netscape){  
            try {  
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
            }catch (e) {  
                alert("该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true");  
            } 
			
			var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);  
			prefs.setCharPref('browser.startup.homepage', 'http://piao.huoche.com');  
        }
		else{
			alert('您用的浏览器不允许直接添加请按CTRL+D试试。');
		}
        
    }
	
}
//加入收藏
function addFavorite(url, title) {
  try {
	url = url || window.location.href;
	title = title || document.title;
	window.external.addFavorite(url, title);
  } catch (e) {
	try {
	  window.sidebar.addPanel(title, url, "");
	} catch (e) {
	  return false;
	}
  }
  return true;
}
